/*
 * Team Java
 * Michael Pogson
 * Michael Satran jr
 * Stephen Hunter
 * 
 * Project 1
 * 
 * 4/26/12
 * 
 */

package httpclient;

import java.util.HashMap;
import java.util.Map;

/**
 * This class gathers all the data needed about a page.
 * @author Michael Pogson, Michael Satran jr, Stephen hunter
 * @version Spring 2012
 *
 */
public class DataGatherer {
	
	/**
	 * The number of words found on the page.
	 */
	private int my_num_of_words;
	
	/**
	 * The number of pages parsed so far.
	 */
	private double my_num_of_pages;
	
	/**
	 * The words that we are looking for.
	 */
	private Map<String, Integer> my_words = new HashMap<String, Integer>();
	
	/**
	 * Creates a data object and puts all the words we are looking for in a map.
	 * @param the_map the map of words to search for.
	 */
	public DataGatherer(final Map<String, Integer> the_map) {
		
		my_words.putAll(the_map);
	}
	
	/**
	 * This goes through the page and counts all the words and occurrences of words we want to search for.
	 * @param the_map all the words found on the page.
	 */
	public void gather(Map<String,Integer> the_map) {
		
		my_num_of_pages++;
		for(String key : the_map.keySet()) {
			
			my_num_of_words += the_map.get(key);
			
		}
		
		
		for(String key : my_words.keySet()) {
			if(the_map.get(key) != null) {
				my_words.put(key, the_map.get(key)+ my_words.get(key));
			}
		}
	}
	
	/**
	 * calculates the total number of words per page so far.
	 * @return returns a number representing the average words.
	 */
	public double averageWordsPerPage() {
		double value;
		value = my_num_of_words/my_num_of_pages;
		return value;
	}
	
	/**
	 * gets the average number of occurrences of the word for a page.
	 * @param the_word the word we want to calculate for
	 * @return returns the average for that word.
	 */
	public double getAverageForWord(String the_word){
		double value;
		value = my_words.get(the_word)/my_num_of_pages;
		return value;
	}
	
	/**
	 * Returns the number of occurrences of a given word
	 * @param the_word the word we want to find for.
	 * @return the number of occurrences of a given word
	 */
	public int getTotalHitsForWord(String the_word) {
		return my_words.get(the_word);
	}
	
	/**
	 * Total number of pages so far
	 * @return the number of pages so far.
	 */
	public double getNumOfPages() {
		return my_num_of_pages;
	}
}
